package command;

import java.util.ArrayList;
import java.util.List;

import model.impl.Station;
import model.impl.StationImpl;
import DTO.DTOManager;
import DTO.StationDTO;

import com.gmail.at.vssabinin.train_transport_system_part1.Protocol;
import com.gmail.at.vssabinin.train_transport_system_part1.ReceivedData;

/**
 * Class represents command interface;
 * It sends all the station objects to the client user. To send them 
 * it uses DTO. If there is any stations it sends message OK and the 
 * list of stations, else sends no and null.
 */
public class ClientManageStation implements Command {

	private Protocol protocol = new Protocol();
	
	@Override
	public void execute(ReceivedData recived) {
		
		model.Station stationImpl = new StationImpl();
		List<Station> stations = stationImpl.findAll();
		List<StationDTO> stationsDTO = new ArrayList<StationDTO>();
		if (stations.isEmpty() || (stations == null)) {
			Protocol.Message answer = this.protocol.new Message("no", null);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		} else {
			for(int i = 0; i < stations.size(); i++) {
				stationsDTO.add((new DTOManager()).createStationDTO(stations.get(i)));
			}
			Protocol.Message answer = this.protocol.new Message("ok", stationsDTO);
			recived.server.send(recived.socket, this.protocol.createMessage(answer));
		}
	}
}
